<template>
    <div>
         <!-- 面包屑导航区 -->
        <el-breadcrumb separator-political="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>党员管理</el-breadcrumb-item>
        <el-breadcrumb-item>政治档案</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card>
             <!-- 搜索 添加 -->
            <el-row :gutter="20">
                <el-col :span="10">
                    <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getPoliticalList">
                        <el-button type="success" slot="append" icon="el-icon-search" @click="getPoliticalList"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="13">
                    <el-button type="primary" @click="showAddPoliticalDialog" style="float:right">添加档案</el-button>
                </el-col>
            </el-row>
            <el-table :data="politicalList" border stripe :header-cell-style="table_header" empty-text="暂无数据">
                <!-- 索引列 -->
                <el-table-column type="index" label="序号" width="50px" align="center"></el-table-column>
                <el-table-column label="学号" prop="stu_id" align="center" width="120px"></el-table-column>
                <el-table-column label="姓名" prop="student.name" align="center"></el-table-column>
                <el-table-column label="入团时间" prop="join_group_time" align="center" width="90px">
                    <template slot-scope="scope">{{scope.row.join_group_time | dateFormat}}</template>
                </el-table-column>
                <el-table-column label="申请入党" prop="application_party_time" align="center" width="90px">
                    <template slot-scope="scope">{{scope.row.application_party_time | dateFormat}}</template>
                </el-table-column>
                <el-table-column label="积极分子" prop="activist_time" align="center" width="90px">
                    <template slot-scope="scope">{{scope.row.activist_time | dateFormat}}</template>
                </el-table-column>
                <el-table-column label="重点发展对象" prop="key_dev_time" align="center" width="110px">
                    <template slot-scope="scope">{{scope.row.key_dev_time | dateFormat}}</template>
                </el-table-column>
                <el-table-column label="推优" prop="promotion_time" align="center" width="90px">
                    <template slot-scope="scope">{{scope.row.promotion_time | dateFormat}}</template>
                </el-table-column>
                <el-table-column label="党课结业" prop="party_class_time" align="center" width="90px">
                    <template slot-scope="scope">{{scope.row.party_class_time | dateFormat}}</template>
                </el-table-column>

                <el-table-column label="党课成绩" prop="party_class_score" align="center" width="90px">
                    <template slot-scope="scope">{{scope.row.party_class_score != 0 ? scope.row.party_class_score : '' }}</template>
                </el-table-column>
                <el-table-column label="预备党员" prop="prob_party_menber_time" align="center" width="90px">
                    <template slot-scope="scope">{{scope.row.prob_party_member_time | dateFormat}}</template>
                </el-table-column>
                <el-table-column label="转正时间" prop="party_member_time" align="center" width="90px">
                    <template slot-scope="scope">{{scope.row.party_member_time | dateFormat}}</template>
                </el-table-column>
                <el-table-column label="介绍人" prop="introducer" align="center" width="120px"></el-table-column>
                <el-table-column label="操作" align="center" fixed="right" width="120px">
                    <template slot-scope="scope">
                        <el-tooltip class="item" effect="dark" content="编辑档案" :enterable="false" placement="top">
                            <el-button type="primary" icon="el-icon-edit" size="mini" circle @click="showEditPoliticalDialog(scope.row)"></el-button>
                        </el-tooltip>
                        <el-tooltip class="item" effect="dark" content="删除档案" :enterable="false" placement="top">
                            <el-button type="danger" icon="el-icon-delete" size="mini" circle @click="removePoliticalById(scope.row)"></el-button>
                        </el-tooltip>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>
        <!-- 分页区域 -->
        <div political="pager-box">
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryInfo.pageNum"
                :page-sizes="[2, 5, 10, 15]"
                :page-size="queryInfo.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                prev-text="上一页"
                next-text="下一页"
                :total="total"
                background
                hide-on-single-page
            ></el-pagination>
        </div>

        <!-- 编辑档案对话框 -->
        <el-dialog :title="Dialog_title" :visible.sync="PoliticalDialogVisible" width="60%" @close="resetForm('PoliticalFormRef')">
            <el-form
                :model="PoliticalForm"
                ref="PoliticalFormRef"
                :rules="PoliticalFormRules"
                label-width="100px"
                :inline="true"
            >
                <el-form-item label="学号" prop="stu_id">
                    <el-select v-model.number="PoliticalForm.stu_id" filterable placeholder="请选择">
                        <el-option
                                v-for="item in stu_id_list"
                                :key="item"
                                :label="item"
                                :value="item">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="入团时间" prop="join_group_time" align="center" width="90px">
                    <el-date-picker v-model="PoliticalForm.join_group_time" type="date" placeholder="选择日期"></el-date-picker>
                </el-form-item>
                <el-form-item label="申请入党" prop="application_party_time" align="center" width="90px">
                    <el-date-picker v-model="PoliticalForm.application_party_time" type="date" placeholder="选择日期"></el-date-picker>
                </el-form-item>
                <el-form-item label="积极分子" prop="activist_time" align="center" width="90px">
                    <el-date-picker v-model="PoliticalForm.activist_time" type="date" placeholder="选择日期"></el-date-picker>
                </el-form-item>
                <el-form-item label="重点发展对象" prop="key_dev_time" align="center" width="90px">
                    <el-date-picker v-model="PoliticalForm.key_dev_time" type="date" placeholder="选择日期"></el-date-picker>
                </el-form-item>
                <el-form-item label="推优" prop="promotion_time" align="center" width="90px">
                    <el-date-picker v-model="PoliticalForm.promotion_time" type="date" placeholder="选择日期"></el-date-picker>
                </el-form-item>
                <el-form-item label="党课结业" prop="party_class_time" align="center" width="90px">
                    <el-date-picker v-model="PoliticalForm.party_class_time" type="date" placeholder="选择日期"></el-date-picker>
                </el-form-item>
                <el-form-item label="党课成绩" prop="party_class_score" align="center" width="90px">
                    <el-input v-model.number="PoliticalForm.party_class_score"></el-input>
                </el-form-item>
                <el-form-item label="预备党员" prop="prob_party_menber_time" align="center" width="90px">
                    <el-date-picker v-model="PoliticalForm.prob_party_member_time" type="date" placeholder="选择日期"></el-date-picker>
                </el-form-item>
                <el-form-item label="转正时间" prop="party_member_time" align="center" width="90px">
                    <el-date-picker v-model="PoliticalForm.party_member_time" type="date" placeholder="选择日期"></el-date-picker>
                </el-form-item>
                <el-form-item label="介绍人" prop="introducer" align="center" width="120px">
                    <el-input v-model="PoliticalForm.introducer"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="PoliticalDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="AddOrUpdatePolitical">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    data () {
        return {
            queryInfo: {
                query: '',
                // 当前页数
                pageNum: 1,
                // 每页显示多少数据
                pageSize: 10,
            },
            total: 0,
            // 所有档案列表
            politicalList: [],
            stu_id_list: [],

            Dialog_state: "",
            Dialog_title: "",

            PoliticalDialogVisible: false,
            PoliticalForm: {},
            PoliticalFormRules: {
                stu_id: [
                    { required: true, message: '请输入学号', trigger: 'blur' }
                ],
            },
        }
    },
    created () {
        this.getPoliticalList();
        this.getStuIDList();
    },
    methods: {
        table_header(row, column, rowIndex, columnIndex){
            return 'background-color: rgb(102,177,255);color: white;font-size:14px;'
        },
        // 监听 pagesize改变的事件
        handleSizeChange (newSize) {
            // console.log(newSize)
            this.queryInfo.pageSize = newSize
            this.getPoliticalList()
        },
        // 监听 页码值 改变事件
        handleCurrentChange (newSize) {
            // console.log(newSize)
            this.queryInfo.pageNum = newSize
            this.getPoliticalList()
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
            this.PoliticalForm = {};
        },   
        async getPoliticalList() {
            const { data: res, status: status } = await this.axios.get('political/getPoliticalList', {params: this.queryInfo});
            if (status !== 200) {
                return this.$message.error('获取档案列表失败！')
            }
            this.politicalList = res.data.politicals;
            this.total = res.data.total;
        },
        async getStuIDList() {
            const {data: res, status: status} = await this.axios.get('student/getStuIDList');
            if (status !== 200) {
                return this.$message.error('获取学生列表失败！')
            }
            this.stu_id_list = res.data;
        },
        showAddPoliticalDialog(){
            this.Dialog_state = "add";
            this.Dialog_title = "添加档案";
            this.PoliticalDialogVisible = true
        },
        // 编辑档案
        async showEditPoliticalDialog (Political) {
            this.Dialog_state = 'edit';
            this.Dialog_title = "编辑档案";
            this.PoliticalForm = Political;
            this.PoliticalDialogVisible = true
        },
        // 添加档案A
        AddOrUpdatePolitical () {
            var political_form = JSON.parse(JSON.stringify(this.PoliticalForm));
            political_form.student = {};
            if (this.Dialog_state === "add") {
                this.$refs.PoliticalFormRef.validate(async valid => {
                    if (!valid) return
                    const {data: res, status: status} = await this.axios.post('political/addPolitical', political_form);
                    if (status !== 201) {
                        this.$message.error('添加档案失败！')
                    }
                    this.$message.success('添加档案成功！');
                    this.getPoliticalList();
                })
            }else {
                this.$refs.PoliticalFormRef.validate(async valid => {
                    if (!valid) return
                    const { data: res, status: status } = await this.axios.put('political/updatePolitical', political_form);
                    if (status !== 200) {
                        this.$message.error('更新档案信息失败！')
                    }
                    this.$message.success('更新档案信息成功！');
                    this.getPoliticalList();
                })
            }
            this.PoliticalDialogVisible = false
        },
        // 删除档案
        async removePoliticalById (Political) {
            const confirmResult = await this.$confirm(
                '此操作将永久删除档案 ' + Political.name +  ',  是否继续?',
                '提示',
                {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }
            ).catch(err => err);
            if (confirmResult !== 'confirm') {
                return this.$message.info('已取消删除')
            }
            const { data: res, status: status } = await this.axios.delete('political/deletePolitical', {params: {"id": Political.id}});
            if (status !== 200) return this.$message.error('删除档案失败！')
            this.$message.success('删除档案成功！')
            this.getPoliticalList();
        },

    } 
}
</script>
<style lang="less" scoped>

</style>